<template>
  <div>
    <el-dialog
      :before-close="cancel"
      :title="info.isAdd?'添加管理员':'编辑管理员'"
      :visible.sync="info.isShow"
      center
    >
      <el-form ref="manger" :model="mangerinfo" :rules="rules">
        <el-form-item prop="rolename" label="所属角色" :label-width="formLabelWidth">
          <el-select v-model="mangerinfo.roleid" placeholder="请选择">
            <el-option disabled label="请选择角色" :value="0"></el-option>
            <el-option
              v-for="item in rolelist"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="username" label="用户名称" :label-width="formLabelWidth">
          <el-input v-model="mangerinfo.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="mangerinfo.password" autocomplete="off"></el-input>
          <p>留空则是不修改</p>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="mangerinfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userlist, useradd, userinfo, useredit } from "../../request/api";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      mangerinfo: {
        roleid: 0,
        username: "",
        password: "",
        status: 1
      },
      rules: {
        username: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ],
        roleid: [{ required: true, message: "请选择角色", trigger: "change" }]
      },
      formLabelWidth: "120px"
    };
  },
  computed: {
    ...mapGetters({
      rolelist: "role/getRole"
    })
  },
  props: ["info"],
  methods: {
    ...mapActions({
      getRole: "role/getRoleActions",
      getManger: "manger/getMangerActions",
      getCount:'manger/getCountActions'
    }),
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    reset() {
      this.mangerinfo = {
        roleid: 0,
        username: "",
        password: "",
        status: 1
      };
    },
    //数据回显：
    lookup(uid) {
      userinfo({ uid }).then(res => {
        if (res.code == 200) {
          this.mangerinfo = res.list;
          this.mangerinfo.uid = uid;
          this.mangerinfo.password = "";
        }
      });
    },
    //封装一个确定事件
    confirm() {
      this.$refs.manger.validate(val => {
        if (val) {
          //验证成功之后，分别调用添加或者编辑接口
          //添加
          if (this.info.isAdd) {
            useradd(this.mangerinfo).then(res => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getManger();
                //调用总条数
                this.getCount();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            useredit(this.mangerinfo).then(res => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getManger();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    }
  },
  mounted() {
    if (this.rolelist.length == 0) {
      this.getRole();
    }
  }
};
</script>

<style  scoped>
</style>
